package com.nw.news.news;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.nw.news.R;
import com.nw.news.base.BaseActivity;
import com.nw.news.news.fragment.FragmentNews;

import java.util.ArrayList;
import java.util.List;

public class NewsActivity extends BaseActivity {
    private TabLayout tabs;
    private ViewPager viewPager;
//    private static NewsAdapter mAdapter;

    private String[] titles = new String[]{"推荐", "国内", "国际", "娱乐", "体育", "军事", "科技", "财经", "游戏", "汽车", "健康"};
    private String[] Types = new String[]{"top", "guonei", "guoji", "yule", "tiyu", "junshi", "keji", "caijing", "youxi", "qiche", "jiankang"};
    private ArrayList<Fragment> fragments = new ArrayList<>();
//    @SuppressLint("HandlerLeak")
//    public static Handler handler = new Handler() {
//        @Override
//        public void handleMessage(@NonNull Message msg) {
//            super.handleMessage(msg);
//            if (msg.what == 10) {
//                mAdapter.updateData(mList);
//                mAdapter.notifyDataSetChanged();
//            }
//        }
//    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_news;
    }

    @Override
    public void init() {
        tabs = findViewById(R.id.tabs);
        viewPager = findViewById(R.id.viewPager1);
        for (int i = 0; i < titles.length; i++) {
            FragmentNews fragment1 = new FragmentNews(Types[i]);
            fragments.add(fragment1);
        }
        MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(pagerAdapter);
        tabs.setupWithViewPager(viewPager);

//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
//        mRecyclerView.setLayoutManager(linearLayoutManager);
//        mList = new ArrayList<>();
//        mAdapter = new TitlesAdapter(mList);
//        mRecyclerView.setAdapter(mAdapter);
//        mAdapter.addItemClickListener(new TitlesAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(Entity.Data ite) {
//                Intent intent = new Intent(NewsActivity.this, ImagerActivity.class);
//                intent.putExtra("url", ite.url);
//                startActivity(intent);
//            }
//        });
//        request();
    }

    static class MyPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragments;
        private String[] titles;

        public MyPagerAdapter(FragmentManager fm, List<Fragment> mFragments, String[] titles) {
            super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
            this.mFragments = mFragments;
            this.titles = titles;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
